#include "MonitorSettings.h"
#include <QDir>
#include <QFileDialog>

MonitorSettings::MonitorSettings(QSettings* s, QWidget *parent)
	: QDialog(parent)
	, settings(s)
{
	ui.setupUi(this);

	UpdateData();

	connect(ui.pushButton_ok, SIGNAL(clicked()), this, SLOT(OnBtnSave()));
	connect(ui.pushButton_cancel, SIGNAL(clicked()), this, SLOT(reject()));
	connect(ui.checkBox_always, &QAbstractButton::toggled, [&](bool bChecked) {
		ui.lineEdit_cycle->setDisabled(bChecked);
	});
}

MonitorSettings::~MonitorSettings()
{
}

void MonitorSettings::UpdateData()
{
	ui.lineEdit_ip->setText(
		settings->value(QSETTINGS_KEY(APP_KEY_SERVER, VALUE_KEY_SERVER_IP)).toString()
	);

	ui.lineEdit_port->setText(
		settings->value(QSETTINGS_KEY(APP_KEY_SERVER,VALUE_KEY_SERVER_PORT), 9090).toString()
	);

	ui.lineEdit_cycle->setText(
		settings->value(QSETTINGS_KEY(APP_KEY_LOCAL, VALUE_KEY_CYCLE_TIMES), 4).toString()
	);

	ui.checkBox_always->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_LOCAL, VALUE_KEY_CYCLE_ALWAYS), false).toBool()
	);

	ui.lineEdit_cycle->setDisabled(ui.checkBox_always->isChecked());

	ui.checkBox_fh->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_FLOW_HI_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);
	ui.checkBox_fl->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_FLOW_LOW_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);

	ui.checkBox_th->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_TEMP_HI_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);
	ui.checkBox_tl->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_TEMP_LOW_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);

	ui.checkBox_ph->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_PRESS_HI_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);
	ui.checkBox_pl->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_PRESS_LOW_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);

	ui.checkBox_dph->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_DPRESS_HI_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);
	ui.checkBox_dpl->setChecked(
		settings->value(QSETTINGS_KEY(APP_KEY_DPRESS_LOW_ALARM, VALUE_KEY_ENABLE), true).toBool()
	);

	ui.lineEdit_flow_hi->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_FLOW_HI_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/flowHiAlarm.wav"
		).toString()
	);
	ui.lineEdit_flow_low->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_FLOW_LOW_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/flowLowAlarm.wav"
		).toString()
	);

	ui.lineEdit_temp_hi->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_TEMP_HI_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/TempHiAlarm.wav"
		).toString()
	);
	ui.lineEdit_temp_low->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_TEMP_LOW_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/TempLowAlarm.wav"
		).toString()
	);

	ui.lineEdit_press_hi->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_PRESS_HI_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/PressHiAlarm.wav"
			).toString()
	);
	ui.lineEdit_press_low->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_PRESS_LOW_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/PressLowAlarm.wav"
		).toString()
	);

	ui.lineEdit_dpress_hi->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_DPRESS_HI_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/DPressHiAlarm.wav"
		).toString()
	);
	ui.lineEdit_dpress_low->setText(
		settings->value(
			QSETTINGS_KEY(APP_KEY_DPRESS_LOW_ALARM, VALUE_KEY_AUDIO_PATH),
			"Sound/DPressLowAlarm.wav"
		).toString()
	);
}

void MonitorSettings::on_pushButton_b1_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_flow_hi->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b2_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_flow_low->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b3_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_temp_hi->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b4_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_temp_low->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b5_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_press_hi->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b6_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_press_low->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b7_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_dpress_hi->setText(file_name);
	}
}

void MonitorSettings::on_pushButton_b8_clicked()
{
	QString file_name = QFileDialog::getOpenFileName(NULL, tr("Select a audio file"), ".", "*.wav");
	if (!file_name.isEmpty())
	{
		ui.lineEdit_dpress_low->setText(file_name);
	}
}

void MonitorSettings::OnBtnSave()
{
	settings->setValue(QSETTINGS_KEY(APP_KEY_SERVER, VALUE_KEY_SERVER_IP), ui.lineEdit_ip->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_SERVER, VALUE_KEY_SERVER_PORT), ui.lineEdit_port->text().toInt());
	settings->setValue(QSETTINGS_KEY(APP_KEY_FLOW_LOW_ALARM, VALUE_KEY_ENABLE), ui.checkBox_fl->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_FLOW_HI_ALARM, VALUE_KEY_ENABLE), ui.checkBox_fh->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_TEMP_LOW_ALARM, VALUE_KEY_ENABLE), ui.checkBox_tl->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_TEMP_HI_ALARM, VALUE_KEY_ENABLE), ui.checkBox_th->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_PRESS_LOW_ALARM, VALUE_KEY_ENABLE), ui.checkBox_pl->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_PRESS_HI_ALARM, VALUE_KEY_ENABLE), ui.checkBox_ph->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_DPRESS_LOW_ALARM, VALUE_KEY_ENABLE), ui.checkBox_dpl->isChecked());
	settings->setValue(QSETTINGS_KEY(APP_KEY_DPRESS_HI_ALARM, VALUE_KEY_ENABLE), ui.checkBox_dph->isChecked());

	settings->setValue(QSETTINGS_KEY(APP_KEY_FLOW_LOW_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_flow_low->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_FLOW_HI_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_flow_hi->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_TEMP_LOW_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_temp_low->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_TEMP_HI_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_temp_hi->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_PRESS_LOW_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_press_low->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_PRESS_HI_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_press_hi->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_DPRESS_LOW_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_dpress_low->text());
	settings->setValue(QSETTINGS_KEY(APP_KEY_DPRESS_HI_ALARM, VALUE_KEY_AUDIO_PATH), ui.lineEdit_dpress_hi->text());

	settings->setValue(QSETTINGS_KEY(APP_KEY_LOCAL, VALUE_KEY_CYCLE_TIMES), ui.lineEdit_cycle->text().toInt());
	settings->setValue(QSETTINGS_KEY(APP_KEY_LOCAL, VALUE_KEY_CYCLE_ALWAYS), ui.checkBox_always->isChecked());
	settings->sync();
	accept();
}
